%%% -------------------------------------------------------------------
%%% Author  : HD
%%% Description :
%%%
%%% Created : 2008-1-21
%%% -------------------------------------------------------------------
-module(sync_server).

-behaviour(gen_server).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% External exports
%% start_link/0	start sync_server
%% send_conf/1	send config file to all remont server
%% send_conf/2	send config file to one remont server
-export([start_link/0,send_conf/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-record(state, {}).

%% ====================================================================
%% External functions
%% ====================================================================
%%
%% start sync_server
%%
start_link() -> 
	gen_server:start_link({local,?MODULE},?MODULE,[],[]).

%% Function: send_conf/1
%% Description: send config to all remont server
%% Returns: ok				|
%%			{error,Reason}
send_conf(ConfigFile)	->
    global:trans({?MODULE,self()},
       	fun() ->
    		gen_server:multi_call(?MODULE,{send,ConfigFile})
        end).

%% ====================================================================
%% Server functions
%% ====================================================================

%% --------------------------------------------------------------------
%% Function: init/1
%% Description: Initiates the server
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%% --------------------------------------------------------------------
init([]) ->
    joinmaster([boot@hdmac]),
    {ok, #state{}}.

%% --------------------------------------------------------------------
%% Function: handle_call/3
%% Description: Handling call messages
%% Returns: {reply, Reply, State}          |
%%          {reply, Reply, State, Timeout} |
%%          {noreply, State}               |
%%          {noreply, State, Timeout}      |
%%          {stop, Reason, Reply, State}   | (terminate/2 is called)
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_call({send,ConfigFile}, From, State) ->
	{FromPid,_ } = From,
	FromNode = node(FromPid),
    if
    	FromNode =:= node()		->
            Reply = ok;
       	true					->
			io:format("recv send ~p ~n",[ConfigFile]),
    		Reply = ok
      end,
    {reply, Reply, State}.

%% --------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_cast(Msg, State) ->
    {noreply, State}.

%% --------------------------------------------------------------------
%% Function: handle_info/2
%% Description: Handling all non call/cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_info(Info, State) ->
    {noreply, State}.

%% --------------------------------------------------------------------
%% Function: terminate/2
%% Description: Shutdown the server
%% Returns: any (ignored by gen_server)
%% --------------------------------------------------------------------
terminate(Reason, State) ->
    ok.

%% --------------------------------------------------------------------
%% Func: code_change/3
%% Purpose: Convert process state when code is changed
%% Returns: {ok, NewState}
%% --------------------------------------------------------------------
code_change(OldVsn, State, Extra) ->
    {ok, State}.

%% --------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Function: joinmaster/1
%% Description: Join to all Master sync server
%% Returns: any (ignored by gen_server)
%% --------------------------------------------------------------------
joinmaster(MasterNotes) when is_list(MasterNotes) ->
    Num = join_1(MasterNotes,0),
    if
    	Num > 0 ->
            io:format("join master sync server ~p ok ~n",[Num]),
            {ok,Num};
        true	->
            io:format("join master sync server error ~n"),
            {error,boot}
    end.

%% --------------------------------------------------------------------
%% Function: join_1/1
%% Description: for each Join to Master sync server
%% Returns: any (ignored by gen_server)
%% --------------------------------------------------------------------
join_1([],Num)           ->
    Num;
join_1([Node|Nodes],Num) ->
    case net_adm:ping(Node) of
        pong  ->
            join_1(Nodes,Num+1);
        pang  ->
            join_1(Nodes,Num)
    end.
